//
//  HKCollectCell.m
//  HKUtilities
//
//  Created by 胡锦涛 on 2018/12/10.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "HKCollectCell.h"
@interface HKCollectCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
/** 列表 */
@property (nonatomic, strong) UICollectionView * collectionView ;
@end
@implementation HKCollectCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self addNoti];
    }
    return self;
}
- (void)addNoti {
    //能滚动
    [HKNotiCenter addObserver:self selector:@selector(acceptMsg:) name:@"goTop" object:nil];
    //不能滚动
    [HKNotiCenter addObserver:self selector:@selector(acceptMsg:) name:@"leaveTop" object:nil];
}
- (void)acceptMsg:(NSNotification*)noti {
    NSString *notificationName = noti.name;
    if ([notificationName isEqualToString:@"goTop"]) {
        _collectionView.scrollEnabled = YES;
        [HKNotiCenter postNotificationName:@"canScroll" object:nil];
    }else if([notificationName isEqualToString:@"leaveTop"]){
         _collectionView.contentOffset = CGPointZero;
        _collectionView.scrollEnabled = NO;
    }
}
- (void)setupUI {
    UICollectionViewFlowLayout * layOut = [[UICollectionViewFlowLayout alloc] init];
    layOut.minimumLineSpacing = 0;
    layOut.minimumInteritemSpacing = 0;
    layOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layOut];
    _collectionView.backgroundColor = kWhiteColor;
    _collectionView.scrollEnabled = NO;
    _collectionView.tag = 4;
    [self.contentView addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = kRANDOM_COLOR;
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH/2, SCREEN_WIDTH/3);
}
@end
